package cn.seqdata.jpa;

import java.util.AbstractMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

/** id=int, name=string */
@MappedSuperclass
public class JpaType extends JpaNamed<Integer> {
	private static final long serialVersionUID = 1L;

	public JpaType() {
		super();
	}

	public JpaType(Integer id) {
		super(id);
	}

	public JpaType(Integer id, String name) {
		super(id, name);
	}

	public JpaType(Entry<? extends Integer, ? extends String> entry) {
		super(entry.getKey(), entry.getValue());
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	@Override
	public Integer getId() {
		return id;
	}

	Map.Entry<Integer, String> toEntry() {
		return new AbstractMap.SimpleImmutableEntry<Integer, String>(getId(), getName());
	}

	@Override
	public int hashCode() {
		return ((getId() == null) ? 0 : getId().hashCode());
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		JpaType other = (JpaType) obj;
		if (getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!getId().equals(other.getId()))
			return false;
		return true;
	}
}
